Action.work = (function(){
    function _loadImgFromUrl(sUrl){
        var WIDTH = PP.WIDTH,
            HEIGHT = PP.HEIGHT,
            context = PP.context;
        var _img = new Image();
        _img.onload = function(){
            var h = _img.height,
                w = _img.width,
                x = 0,
                y = 0;
            if(h > HEIGHT || w > WIDTH){
                var r0 = h/HEIGHT,
                    r1 = w/WIDTH;
                if(r0 > r1){
                    h = HEIGHT;
                    w = w/r0;
                }else{
                    w = WIDTH;
                    h = h/r1;
                }
            }
            x = (WIDTH - w)/2;
            y = (HEIGHT - h)/2;
            context.drawImage(_img, x, y, w, h);
            $.extend(PP, {
                x : x,
                y : y,
                w : w,
                h : h
            });

            PP.isPicLoad = true;
        };
        _img.src = sUrl;
    }
    function _loadImg(e){
        $("#dialog_album").dialog('close');
        _loadImgFromUrl(e.target.src);
    }
    function openAlbum(){
        $("#dialog_album").dialog({
            title: '选择照片',
            autoOpen: false,
            show: "fade",
            hide: "fade",
            width: 400,
            height: 350,
            resizable: false,
            modal: true
        }).dialog("open");
    }
    function openLocal(){
        Action.util.alert("抱歉，此功能正在建设中...");
    }

    function save(){
        /*
        alert((PP.canvas.toDataURL()).length);
        var a = document.createElement("div");

        a.innerHTML = PP.canvas.toDataURL();
        document.body.appendChild(a);
        a.style.width="800px";
        */
        var cx = document.createElement("canvas");
        cx.width = PP.w;
        cx.height = PP.h;
        var uploadData = PP.context.getImageData(PP.x,PP.y,PP.w,PP.h);
        (cx.getContext("2d")).putImageData(uploadData,0,0);


        

        var canvasData = cx.toDataURL("image/png");
        var ajax = new XMLHttpRequest();
        ajax.open("POST",'upload.php',false);
        ajax.setRequestHeader('Content-Type', 'application/upload');
        ajax.onreadystatechange = updatePage;
        ajax.send(canvasData );


        function updatePage(request){
            if (ajax.readyState == 4)
            {
                if (ajax.status == 200) {
                    var a = jQuery.parseJSON(ajax.responseText);
                    var html = '图片上传成功！<br><img width="'
                        +PP.w+'" height="'
                        +PP.h+'" src="test.png">';
                    $( "#msg" ).empty().append(html).dialog({
                        title: "提示",
                        modal: true,
                        width:PP.w+150,
                        height:PP.h+180,
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
                } else
                    alert("status is " + ajax.status);
            }
        }
    }

    

    function dragenter(e) {
        e.stopPropagation();
        e.preventDefault();
    }

    function dragleave(e) {
        e.stopPropagation();
        e.preventDefault();
    }

    function dragover(e) {
        e.stopPropagation();
        e.preventDefault();
    }

    function loadSysPic(file){
        var img = new Image();
        var dataURLReader = new FileReader();
        dataURLReader.onloadend = function() {
            /*
            img.onload = function(){
                PP.context.drawImage(img,0,0,img.width,img.height);
            };
            img.src = ;
            */
            _loadImgFromUrl(dataURLReader.result);
        }

        dataURLReader.readAsDataURL(file);
    }
    function drop(event){
        event.stopPropagation();
        event.preventDefault();
        var imageType = /image\/jpeg/;
        loadSysPic(event.dataTransfer.files[0]);
    }

    function selectPic(){
        $("#selectPicDialog").dialog({
            title: '选择照片',
            autoOpen: false,
            show: "fade",
            hide: "fade",
            width: 400,
            height: 300,
            resizable: false,
            modal: true
        }).dialog("open");
        $("#selectPicDialog>button").click(function(){
            $("#selectPicDialog").dialog("close");
            openAlbum();
        });
    }

    function loadMyPic(e){
        $("#selectPicDialog").dialog("close");
        loadSysPic(e.files[0]);
    }

    function init(){
        $("#dialog_album").hide();
        var ops = $('#op>li');
        $(ops[1]).click(openAlbum);
        $(ops[3]).click(save);

        $.each($('#dialog_album img'), function(i,el){
            $(el).click(_loadImg);
        });
        
        var canvasBox = document.getElementById("canvasBox");
        canvasBox.addEventListener("drop", drop, true);
        canvasBox.addEventListener("dragover", dragover, true);
        

        selectPic();
    }

    return {
        init : init,
        openAlbum : openAlbum,
        loadMyPic : loadMyPic
    }
})();